package com.jss.lanouhn.fristapp.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jss.lanouhn.fristapp.Interface.OnItemClickLister;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.modles.CategoryActivityData;
import com.squareup.picasso.Picasso;

/**
 * Created by lanouhn on 16/9/12.
 */
public class CategoryActivityAdapter extends RecyclerView.Adapter<CategoryActivityAdapter.CategoryViewHolder> {

    private Context context;
    private CategoryActivityData data;
    private OnItemClickLister onItemClickLister;

    public void setOnItemClickLister(OnItemClickLister onItemClickLister){
        this.onItemClickLister=onItemClickLister;
    }

    public CategoryActivityAdapter(Context context, CategoryActivityData data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_category_activity, null);
        CategoryViewHolder holder = new CategoryViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final CategoryViewHolder holder, int position) {
        CategoryActivityData.DataBean.ColumnsBean bean=data.getData().getColumns().get(position);
        holder.tv1.setText(bean.getTitle());
        holder.tv2.setText(bean.getSubtitle());
        holder.tv3.setText(bean.getAuthor());
        Picasso.with(context).load(bean.getCover_image_url()).into(holder.iv);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    int pos=holder.getLayoutPosition();
                    onItemClickLister.onItemClick(holder.itemView,pos);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.getData().getColumns().size();
    }

    class CategoryViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv;
        private TextView tv1,tv2,tv3,tv4;
        public CategoryViewHolder(View itemView) {
            super(itemView);
            iv= (ImageView) itemView.findViewById(R.id.iv_item_category);
            tv1= (TextView) itemView.findViewById(R.id.tv1_item_category);
            tv2= (TextView) itemView.findViewById(R.id.tv2_item_category);
            tv3= (TextView) itemView.findViewById(R.id.tv3_item_category);
            tv4= (TextView) itemView.findViewById(R.id.tv4_item_category);
        }
    }
}
